package com.lagou.rocketmq.controller;

import com.lagou.rocketmq.entity.Product;
import com.lagou.rocketmq.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 创建商品，设置商品个数
     * @param stockNum
     * @return
     */
    @PostMapping("/product/{stockNum}")
    public Product order(@PathVariable(value = "stockNum") Integer stockNum) {

        Product product = new Product();
        product.setStockNum(stockNum);
        productService.save(product);

        redisTemplate.opsForValue().set("product:stock:" + product.getProductId(), stockNum.toString());

        return product;
    }
}
